<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/2 14:00
 * Modified Time: 2019/7/2 14:00
 * Version: 2.0
 */

namespace App\Modules\Inventory\Repositories;


use App\Exceptions\AllValidatorException;
use App\Models\Goods;
use App\Modules\Inventory\Validators\GoodsValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;

class GoodsRepository extends BaseRepository
{
    public function model()
    {
        return Goods::class;
    }

    public function validator()
    {
        return GoodsValidator::class;
    }

    /**
     * 新增商品
     * @param $store_id
     * @param $cat_id
     * @param $name
     * @param $small_name
     * @param $buying_price
     * @param $market_price
     * @param $royalty
     * @param $volume
     * @param $specs
     * @param $is_sell
     * @param $image
     * @return mixed
     */
    public function createGoods(
        $store_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty,
        $volume, $specs, $is_sell, $image
    )
    {
        $data = [
            'store_id' => $store_id,
            'cat_id' => $cat_id,
            'name' => $name,
            'small_name' => $small_name,
            'buying_price' => $buying_price,
            'volume' => $volume,
            'specs' => $specs,
            'is_sell' => $is_sell,
            'image' => $image
        ];

        if($is_sell){
            $data['market_price'] = $market_price;
            $data['royalty'] = $royalty;
        }

        return $this->create($data);
    }

    /**
     * 编辑商品
     * @param $goods_id
     * @param $cat_id
     * @param $name
     * @param $small_name
     * @param $buying_price
     * @param $market_price
     * @param $royalty
     * @param $volume
     * @param $specs
     * @param $is_sell
     * @param $image
     * @return mixed
     */
    public function editGoods($goods_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty, $volume, $specs, $is_sell, $image){

        $this->checkExist($goods_id);

        $data = [];
        if($cat_id){
            $data['cat_id'] = $cat_id;
        }
        if($name){
            $data['name'] = $name;
        }
        if($small_name){
            $data['small_name'] = $small_name;
        }
        if($buying_price){
            $data['buying_price'] = $buying_price;
        }
        if($market_price){
            $data['market_price'] = $market_price;
        }
        if($royalty){
            $data['royalty'] = $royalty;
        }
        if($volume){
            $data['volume'] = $volume;
        }
        if($specs){
            $data['specs'] = $specs;
        }
        if($is_sell){
            $data['is_sell'] = $is_sell;
        }
        if($image){
            $data['image'] = $image;
        }

        return $this->update($data,$goods_id);
    }

    public function delGoods($goods_id)
    {
        $this->checkExist($goods_id);

        return $this->delete($goods_id);
    }
    /**
     * 检测商品是否存在
     * @param $goods_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function checkExist($goods_id){
        $goods = $this->find($goods_id);
        if(empty($goods)){
            throw new AllValidatorException(['msg' => '商品不存在']);
        }
        return $goods;
    }


	/**
	 * 获取商品信息
	 * @param $goodsId
	 */
	public static function getGoodsInfo($goodsId){
    	return empty($goodsId) ? [] :  Goods::getGoodsInfoById($goodsId);
	}

}